require "Net/send_Msg"
require "Net/TrialHandel"
require 'View/UIBase/UIBase'

UINextBossPreview = UIBase.New(PanelNames.UINextBossPreview);
local change_type = nil
function UINextBossPreview.Init(func, _change_type)
    change_type = _change_type or 1
    UIBase.Init(UINextBossPreview, func);
end
local skills = {};
--定义控件
function UINextBossPreview:OnCreate()
    self.rewardCount = 1;
    self.bossIcon = self:GetChild("BossModelPos");
    self.nextBossTimeText = self:GetChild("NextBossTime");
    self.bossDecTip = self:GetChild("NextBossTips");
    self.skillList = self:GetChild("NextBossSkill");
    self.nextBossName = self:GetChild("NextBossName");
    self.closeBgBtn = self:GetChild("BgClose");
    self.tipsShowController = self:GetController("tipsShow");
    self.IfAffixController = self:GetController("IfAffix");
    self.skillName = self:GetChild("BossSkillName");
    self.skillDec = self:GetChild("BossSkillTips");
    self.closeTipBtn = self:GetChild("closeTipsBtn");
    self.closeBtn = self:GetChild("Button_Confirm");
    
    self.titleSkill = self:GetChild("titleSkill");
    self.titleSkillName = self.titleSkill:GetChild("Word");
    self.titleSkillDecName = self.titleSkill:GetChild("WordName");
    self.titleSkillDecTips = self.titleSkill:GetChild("WordTips");

    self.curTime = 0;
    self.Role = nil;
    self.drawWrapper = nil;
    self:addEvent();
    if change_type == 1 then
        self:RegisterEvent(Config.EventType.Trials_Chaos_Info, self.refresh);
    else
        -- todo
    end

    --self.CloseFom();
end

function UINextBossPreview:OnEnable()
    self:refresh()
end

function UINextBossPreview:OnDisable()
    if self.idleTimer then
        self.idleTimer:Stop();
    end
end

function UINextBossPreview:refresh()
    local self = UINextBossPreview;
    local msg = nil
    if change_type == 1 then
        msg = logicMgr.TrialsManager.GetTrialsInfo()
    else
        -- todo
    end
    if msg == nil then return end

    local cfg = logicMgr.TrialsManager.GetTrialsConfig()[tostring(msg.nextTrialsId)];
    local data = G.dataTable["t_hero"][tostring(cfg.f_TrialsBossId)]
    self.curTime = msg.endTime - HelperFunc.GetServerTime();
    self:refreshTime();

    local titleSkillData = G.dataTable["t_skillinfo"][tostring(msg.nextSkillId)];
    if titleSkillData ~= nil then
        self.titleSkillName.text = getLanguage(titleSkillData.f_SkillName);
        self.titleSkillDecName.text = getLanguage(titleSkillData.f_SkillName);
        self.titleSkillDecTips.text = getLanguage(titleSkillData.f_Desc);
        local titleSkillShowData = G.dataTable["t_skillshow"][tostring(msg.nextSkillId)];
        if titleSkillShowData ~= nil then
            self.titleSkill:GetChild("n12"):GetChild("icon").url = "ui://SkillAtlas/" .. titleSkillShowData.f_SkillIcon;
        end
    else
        self.titleSkill.visible = false;
    end 
    self.nextBossName.text = getLanguage(data.f_HeroName);
    self.bossDecTip.text = getLanguage(cfg.f_TrialsBossIntroduce);
    local url = HeroManager.GetHeroDrawingRolePath(data.f_HeroID) 
    if self.idleTimer == nil then
        self.idleTimer = Timer.New(self.refreshTime, 1, -1, true);
    else
        self.idleTimer:Reset(self.refreshTime, 1, -1, true);
    end
    self.idleTimer:Start();

    self.bodyUrl = url;
    if (self.Role ~= nil) then
        destroyImmediate(self.Role);
        self.Role = nil
    end
    resMgr:LoadModelPrefab(url, function(go)
        if self.drawWrapper == nil then
            self.drawWrapper = GoWrapper.New();

        end
        self.Role = newObject(go);
        self.Role:SetActive(true)
        self.Role:AddComponent(TCanvas)
        self.Role:AddComponent(TChangeSortingOrder)
        self.Role.name = url;
        local aniObj = self.Role.transform:Find("Animation").gameObject;
        Util.PlayAction(aniObj, "fidle", true, 4);
        --local roleCfgId = data.f_HeroModel
        --local skinName = (G.dataTable["t_roleconfig"][roleCfgId] or {}).skin;
        --if skinName ~= nil and skinName ~= '' then
        --    Util.SetSkin(aniObj, skinName);
        --end
        local cfg = logicMgr.TrialsManager.GetTrialsConfig()[tostring(msg.nextTrialsId)];
        logicMgr.EquipManager.setWeaponSkin(aniObj, cfg.f_TrialsBossId, false);

        self.drawWrapper:SetWrapTarget(self.Role, true)
        Util.Lua_SetLocalPositionXYZ(self.Role.transform,0,0,0)
        Util.Lua_GetLocalPositionXYZ(aniObj.transform,0,0,0)
        Util.Lua_SetLocalScaleXYZ(self.Role.transform,240, 240,1)
        Util.Lua_SetLocalRotationXYZ(aniObj.transform, 0, 0, 0);
        self.bossIcon:SetNativeObject(self.drawWrapper)
    end)

    local skill = nil;
    local count = 2;
    if #skills <= 0 then
        while true do
            skill = data["f_HeroSkill" .. count];
            if skill == nil or skill == 0 then
                break ;
            end
            table.insert(skills, skill);
            count = count + 1;
        end
    end
    skillCount = 0
    self.skillList.itemRenderer = self.rendererBossSkill;
    self.skillList.numItems = #skills;

end

function UINextBossPreview:OnDestroy()
    self:UnRegisterEvent(Config.EventType.Trials_Chaos_Work_Rank, self.refresh, self);
    destroy(self.Role)
    self.Role = nil
end

function UINextBossPreview:addEvent()
    self:RegisterOnClick(self.rankListBtn, self.OpenRank)
    self:RegisterOnClick(self.closeBgBtn, self.onCloseClick)
    self:RegisterOnClick(self.closeBtn, self.onCloseClick)
    self:RegisterOnClick(self.titleSkill,self.onTitleSkill);
    self:RegisterOnClick(self.closeTipBtn, function()
        self.tipsShowController.selectedIndex = 0;
    end)
end

function UINextBossPreview:OpenRank()
    showUI(PanelNames.UINextBossPreview);
end

function UINextBossPreview:removeEvent()
    ----bt_SetBtn.onClick:Clear();
    --self.backBtn.onClick:Clear();
    --self.bg.onClick:Clear();
    --self.lastBtn.onClick:Clear();
    --self.nextBtn.onClick:Clear();
    --self.titleDecBackBtn.onClick:Clear();
end

function UINextBossPreview:onTitleSkill()
    local msg = logicMgr.TrialsManager.GetTrialsInfo();
    local titleSkillData = G.dataTable["t_skillinfo"][tostring(msg.skillId)];
    if titleSkillData == nil then
        printError("t_skillinfo is nil, skillId ==", msg.skillId)
        return
    end
    self.tipsShowController.selectedIndex = 1;
    self.IfAffixController.selectedIndex = 1;
    self.skillName.text = getLanguage(titleSkillData.f_SkillName);
    self.skillDec.text = getLanguage(titleSkillData.f_Desc);
end

function UINextBossPreview:onCloseClick()
    closeUI(PanelNames.UINextBossPreview);
end

function UINextBossPreview:rendererBossSkill(obj)
    local self = UINextBossPreview;
    skillCount = skillCount + 1;
    local skillId = skills[skillCount];
    local skillData = G.dataTable["t_skillinfo"][tostring(skillId)];

    obj.onClick:Clear();
    obj.onClick:Add(function()
        self.tipsShowController.selectedIndex = 1;
        self.IfAffixController.selectedIndex = 0;
        self.skillName.text = getLanguage(skillData.f_SkillName);
        self.skillDec.text = getLanguage(skillData.f_Desc);
    end);

    obj:GetChild("icon"):GetChild("icon").url = "ui://SkillAtlas/" .. skillData.f_SkillIcon;

end

function UINextBossPreview:refreshTime()
    local self = UINextBossPreview;
    self.curTime = self.curTime - 1;
    if self.curTime > 0 then
        self.nextBossTimeText.text = timeToTimeFormat(self.curTime);
    else
        self.nextBossTimeText.text = "00:00:00";
    end
end

return UINextBossPreview;